Util.statistics('setting');
var rowid = -1;
var userInfo;

var result = [];
var menuname;
var menuurl;
var sort;

var url = document.location.toString();
var imgurl = "http://" + url.split('/')[2].split(':')[0] + ':2000/';

$(function () {
    userInfo = Util.getUserInfo();
    if (userInfo) {
        $('#loginText').html('当前登陆账号为：');
        $('a[data-target="#logindiv"]').html(userInfo.userName);
    }
});


$('#addIndex').on('click', function () {
    if (!userInfo) {
        Util.tips('请先登陆');
        return;
    }
    reset();
});

$('#editIndex').on('click', function () {
    if (!userInfo) {
        Util.tips('请先登陆');
        return;
    }
    reset();

    $('#confirm').html('修改').attr('btntype', '1');
    $('#delIndex').css('display', 'block');

    getIndex();
});

function reset() {
    $('#menuname').val('');
    $('#menuurl').val('');
    $('#menuimgurl').attr('src', '');
    $('#sort').val('');
    $('#delIndex').css('display', 'none');

    if ($('#list').attr('class') == 'collapse show') {
        $('#list').attr('class', 'collapse');
    }

    $('#confirm').html('新增').attr('btntype', '0');

    rowid = -1;

    $('.config').hide();
    $('.index').show();
}


function getIndex() {
    $('#list ul').empty();
    getIndexInfo(function (result) {
        $.each(result, function (i, v) {
            var html =
                '<li class="list-inline-item">' +
                '<a href="javascript:void(0);" rowid="' + v.id + '">' + v.menuName + '</a>' +
                '</li>';
            $('#list ul').append(html);
        });
    });
}

function getIndexInfo(callback) {
    var param = {
        userid: userInfo.id
    };
    Util.postJson("./common-server/user/api/v1/index", param, function (response) {
        if (response.code != 0) {
            Util.tips(response.message);
            return;
        }
        result = response.result;
        if (localStorage) {
            localStorage.setItem('index_' + userInfo.id, JSON.stringify(result));
        }
        callback(result);
    });
}

$('#list').on('click', 'a[rowid]', function () {
    // $('#editIndex').trigger('click');

    rowid = $(this).attr('rowid');


    var res = {};
    $.each(result, function (i, v) {
        if (v.id == rowid) {
            res = v;
            return false;
        }
    });

    $('#menuname').val(res.menuName);
    $('#menuurl').val(res.menuUrl);
    $('#menuimgurl').attr('src', res.menuImgUrl);
    $('#sort').val(res.sort);

});

function check() {
    if (!userInfo) {
        Util.tips('请先登陆');
        return false;
    }

    menuname = $('#menuname').val();
    if (menuname == "") {
        Util.tips('请输入网址名称');
        return false;
    }

    menuurl = $('#menuurl').val();
    if (menuurl == "") {
        Util.tips('请输入网址链接');
        return false;
    }

    // menuimgurl = $('#menuimgurl').val();
    // if (menuimgurl == "") {
    //     Util.tips("请选择网址对应图片");
    //     return false;
    // }

    sort = $('#sort').val();
    if (sort == "") {
        Util.tips('请输入排序');
        return false;
    }

    return true;
}


$('#menuimg').on('change', function () {
    var reader = new FileReader();
    reader.readAsDataURL($('#menuimg')[0].files[0]);
    reader.onload = function (event) {
        $('#menuimgurl').attr('src', event.target.result);
    }
});

$('#backgroundimg').on('change', function () {
    var reader = new FileReader();
    reader.readAsDataURL($('#backgroundimg')[0].files[0]);
    reader.onload = function (event) {
        $('#backgroundimgurl').attr('src', event.target.result);
    }
});

function addIndex() {
    if (!check()) {
        return;
    }

    var param = {
        menuname: menuname,
        menuurl: menuurl,
        menuimgurl: $('#menuimgurl').attr('src'),
        sort: sort,
        userid: userInfo.id
    };
    Util.postJson("./common-server/user/api/v1/addIndex", param, function (response) {
        if (response.code != 0) {
            Util.tips(response.message);
            return;
        }
        Util.tips("新增成功，返回到导航页刷新页面即可看到效果哦");
        reset();
        if (localStorage) {
            localStorage.removeItem('index_' + userInfo.id);
        }
    });


}

function editIndex() {
    if (rowid == -1) {
        Util.tips('请选择一条记录修改');
        return;
    }

    if (!check()) {
        return;
    }

    var param = {
        rowid: rowid,
        menuname: menuname,
        menuurl: menuurl,
        menuimgurl: $('#menuimgurl').attr('src'),
        sort: sort,
        userid: userInfo.id
    };
    Util.postJson("./common-server/user/api/v1/editIndex", param, function (response) {
        if (response.code != 0) {
            Util.tips(response.message);
            return;
        }
        Util.tips("修改成功，返回到导航页刷新页面即可看到效果哦");
        $('#editIndex').trigger('click');
        if (localStorage) {
            localStorage.removeItem('index_' + userInfo.id);
        }
    });
}

function delIndex() {
    if (rowid == -1) {
        Util.tips('请选择一条记录删除');
        return;
    }

    if (confirm('确认删除吗？')) {
        var param = {
            rowid: rowid,
            userid: userInfo.id
        };
        Util.postJson("./common-server/user/api/v1/delIndex", param, function (response) {
            if (response.code != 0) {
                Util.tips(response.message);
                return;
            }
            Util.tips("删除成功");
            $('#editIndex').trigger('click');
            if (localStorage) {
                localStorage.removeItem('index_' + userInfo.id);
            }
        });
    }

}


$('#confirm').on('click', function () {
    var btntype = $(this).attr('btntype');
    if (btntype == 0) {
        addIndex();
    }
    if (btntype == 1) {
        editIndex();
    }
});

$('#delIndex').on('click', function () {
    delIndex();
});

$('#submit').on('click', function () {
    var username = $('#name').val();
    if (username == "") {
        Util.tips("请填写用户名");
        return;
    }

    var password = $('#pwd').val();
    if (password === "") {
        Util.tips("请填写密码");
        return;
    }

    var param = {
        username: username,
        password: password
    };
    Util.postJson("./common-server/user/api/v1/login", param, function (response) {
        if (response.code != 0) {
            Util.tips(response.message);
            return;
        }

        $('#logindiv').modal('hide');
        $('a[data-target="#logindiv"]').html(response.userInfo.userName);

        $('.login a:eq(0)').css('display', 'none');
        $('.login a:eq(1)').css('display', 'block');

        $('#loginname').html(response.userInfo.userName);

        userInfo = response.userInfo;

        // 设置cookie的有效期为10天
        // $.cookie("userInfo", JSON.stringify(response.userInfo), {
        //     expires: 10
        // });

        // getIndexInfo();
        if (localStorage) {
            localStorage.setItem("userInfo", JSON.stringify(response.userInfo));
        }
    });
});


$("#config").on("click", function () {
    if (!userInfo) {
        Util.tips('请先登陆');
        return;
    }

    $('.index').hide();
    $('.config').show();

    if ($('#list').attr('class') == 'collapse show') {
        $('#list').attr('class', 'collapse');
    }

    var param = {
        userid: userInfo.id
    };
    Util.postJson("./common-server/user/api/v1/getConfig", param, function (response) {
        if (response.code != 0) {
            Util.tips(response.message);
            return;
        }

        var result = response.result;
        if (result.length > 0) {
            var config = result[0].config;
            config = JSON.parse(config);
            var weatherSwitch = config.weatherSwitch;
            if (weatherSwitch) {
                $("#weatherswitch").attr('checked', true);
            }
            $("#weatherswitch").bootstrapSwitch();

            var weatherCity = config.weatherCity;
            if (weatherCity) {
                $("#weathercity").val(weatherCity);
            }

            var searchInputShow = config.searchInputShow;
            if (searchInputShow) {
                $("#searchinputshow").attr('checked', true);
            }
            $("#searchinputshow").bootstrapSwitch();

            var logoShow = config.logoShow;
            if (logoShow) {
                $("#logoshow").attr('checked', true);
            }
            $("#logoshow").bootstrapSwitch();

            var searchEngines = config.searchEngines;
            $('#searchengines').find('option[value="' + searchEngines + '"]').attr('selected', true);

            var backgroundImgShow = config.backgroundImgShow;
            if (backgroundImgShow) {
                $("#backgroundimgshow").attr('checked', true);
            }
            $("#backgroundimgshow").bootstrapSwitch();

            var backgroundImgUrl = config.backgroundImgUrl;
            if (backgroundImgUrl) {
                var url = imgurl + backgroundImgUrl.split("/")[5];
                // var reader = new FileReader();
                // reader.readAsDataURL(url);
                // reader.onload = function (event) {
                //     $('#backgroundimgurl').attr('src', event.target.result);
                // }
                $('#backgroundimgurl').attr('src', url);
            }
        }
    });

});

// $("#backgroundimgshow").on('click', function () {
//     if ($(this).is(":checked")) {
//         $("label[for=backgroundimg]").show();
//     } else {
//         $("label[for=backgroundimg]").hide();
//     }
// });

$("#editconfig").on("click", function () {
    var weatherswitch = $("#weatherswitch").is(':checked');
    var weathercity = $("#weathercity").val();
    var searchinputshow = $("#searchinputshow").is(':checked');
    var searchengines = $("#searchengines").val();

    var logoshow = $("#logoshow").is(':checked');

    var backgroundimgshow = $("#backgroundimgshow").is(':checked');
    var backgroundimgurl = $('#backgroundimgurl').attr('src');
    var param = {
        userid: userInfo.id,
        weatherSwitch: weatherswitch,
        weatherCity: weathercity,
        searchInputShow: searchinputshow,
        searchEngines: searchengines,
        logoShow: logoshow,
        backgroundImgShow: backgroundimgshow,
        backgroundImgUrl: backgroundimgurl
    };
    Util.postJson("./common-server/user/api/v1/editConfig", param, function (response) {
        if (response.code != 0) {
            Util.tips(response.message);
            return;
        }
        Util.tips("修改成功，返回到导航页刷新页面即可看到效果哦");
        $("#config").trigger("click");
    });

});

